package com.xj.toOffer.nowCoder;

import com.alibaba.fastjson.JSON;

/**
 * @author: xujiang
 * @createTime: 2021/7/21 13:59:51
 */
public class JZ13 {
    public static void main(String[] args) {
        JZ13 jz13 = new JZ13();
        System.out.println(JSON.toJSONString(jz13.reOrderArray(new int[]{1,2,3,4})));
    }
    public int[] reOrderArray (int[] array) {
        // write code here
        if (array == null || array.length == 0) {
            return array;
        }
        return mergeArr(getOddArr(1, array), getOddArr(2, array));

    }
    public int[] getOddArr(int type, int [] array) {
        int [] arr = new int[array.length];
        int k = 0;
        if (type == 1) {// 奇数
            for (int i = 0; i < array.length; i++) {
                if (array[i] % 2 != 0) {
                  arr[k++] = array[i];
                }
            }
        }
        if (type == 2) {// 奇数
            for (int i = 0; i < array.length; i++) {
                if (array[i] % 2 == 0) {
                  arr[k++] = array[i];
                }
            }
        }
        int[] result = new int[k];
        for (int i = 0; i < k; i++) {
            result[i] = arr[i];
        }
//        return new int[]{};
        return result;
    }

    public int[] mergeArr(int [] arr1, int [] arr2) {
        int[] arr3 = new int[arr1.length + arr2.length];
        int  k = 0;
        for (int i = 0; i < arr1.length; i++) {
            arr3[k++] = arr1[i];
        }
        for (int i = 0; i < arr2.length; i++) {
            arr3[k++] = arr2[i];
        }
        return arr3;
    }
}
